package com.ux.example.particle
{
	import com.greensock.TweenMax;
	import com.greensock.easing.Bounce;
	import com.greensock.easing.Quint;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class ParticleCircleExample extends Sprite
	{
		private var numOfParticle : int = 120;
		
		private var pList : Vector.<CircleClass> = new Vector.<CircleClass>();
		private var pTargetList : Vector.<Point> = new Vector.<Point>();
		private var groundList : Vector.<Point> = new Vector.<Point>();
		private var circleContainer : Sprite;
		
		public function ParticleCircleExample()
		{
			super();
			
			circleContainer = new Sprite();
			addChild( circleContainer );
			
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++) 
			{
				var p :CircleClass = new CircleClass();
				circleContainer.addChild( p );
				pList.push( p );
			}
			
			circleContainer.x = 200;
			circleContainer.y = 200;
			stage.addEventListener(MouseEvent.CLICK, onClick );
			
			init();
		}
		
		private var isMove : Boolean = false;
		private function onClick( e:MouseEvent ):void
		{
			if( isMove == false ) movePosition();
			else moveComplete();
			
			isMove = !isMove;
				
		}
		
		private function init():void
		{
			var targetList : Vector.<Point> = getCirclePoint( 50, 0, 0 );
			
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++)
			{
				TweenMax.to( pList[i] , 1, {x:targetList[i].x, y:targetList[i].y , ease:Quint.easeOut, delay:i*0.005} );
			}
			
			TweenMax.to( circleContainer, 3, {rotation:90} );
		}
		
		private function movePosition():void
		{
			TweenMax.to( circleContainer, 0, {rotation:0} );
			
			var targetList : Vector.<Point> = getWavePoint( 50, 0, 0, 2 );
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++)
			{
				TweenMax.to( pList[i] , 1, {x:targetList[i].x, y:targetList[i].y , ease:Bounce.easeOut, delay:i*0.005 } );
			}
			
			var dur : Number = (1+numOfParticle*0.005);
			TweenMax.to( circleContainer, dur , { x:0, ease:Quint.easeInOut, onComplete:move } );
		}
		
		
		private function moveComplete():void
		{
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++)
			{
				TweenMax.to( pList[i] , 1, {x:100, y:0 , ease:Bounce.easeOut, delay: Math.abs( (i-numOfParticle/2)/60 ) } );
			}
		}
		
		public function move():void
		{
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++) 
			{
				TweenMax.to( pList[i], 1, {y:pList[i].y-50, ease:Bounce.easeOut, delay: Math.abs( (i-numOfParticle/2)/60 ), yoyo:true, repeat:-1 } ); //, tint:randRange(0,16777216), scaleX:randRange(0,5), scaleY:randRange(0,5) 
			}
		}
		
		
		private var waveNum : int = 0;
		private function onEnter( e:Event ):void
		{
			var radius : Number      = 50;
			var theta : Number       = 0;
			var speed : Number       = 3;
			var angle : Number       = 0;
			
			var len:int = pList.length;
			for (var i:int = 0; i < len; i++) 
			{
				angle += speed*3;
				theta = angle*2*Math.PI/360;
				pList[getIndex(i+waveNum)].y += (radius * Math.sin( theta )-pList[getIndex(i+waveNum)].y)*0.6;
			}
			
			waveNum++;
			
			if( waveNum >= len )
			{
				waveNum = 0;
			}
		}
		
		
		private function getIndex( i:int ):int
		{
			return i >= numOfParticle ? i-numOfParticle : i;
		}
		
		
		public function getWavePoint( _radius:Number = 50, _cx:Number = 0, _cy:Number = 0, numOfrotatory:int = 2 ):Vector.<Point>
		{
			var angle : Number       = 0;
			var theta : Number       = 0;
			var speed : Number       = 360/numOfParticle * numOfrotatory;
			var cx : Number          = _cx;
			var cy : Number          = _cy;
			var radius : Number      = _radius;
			
			var vec : Vector.<Point> = new Vector.<Point>();
			
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++) 
			{
				var _x : Number = 0;
				var _y : Number = 0;
				
				angle += speed;
				theta = angle*2*Math.PI/360;
				
				_x = stage.stageWidth*i/numOfParticle;
				_y = radius * Math.sin( theta );
				
				vec.push( new Point( _x, _y ) );
			}
			
			return vec;
		}
		
		
		
		/**
		 * @param num 0 ~ 360 
		 * @return 
		 */		
		public function getCirclePoint( _radius:Number = 50, _cx : Number = 0, _cy : Number = 0 ):Vector.<Point>
		{
			var angle : Number       = 0;
			var theta : Number       = 0;
			var speed : Number       = 360/numOfParticle;
			var radius : Number      = _radius;
			var cx : Number          = _cx;
			var cy : Number          = _cy;
			
			var vec : Vector.<Point> = new Vector.<Point>();
			
			var len:int = numOfParticle;
			for (var i:int = 0; i < len; i++) 
			{
				var _x : Number = 0;
				var _y : Number = 0;
				
				angle += speed;
				theta = angle*2*Math.PI/360;
				
				_x = radius * Math.cos( theta ) + cx; 
				_y = radius * Math.sin( theta ) + cy;
				
				vec.push( new Point( _x, _y ) );
			}
			
			return vec;
		}
		
		
		private function randRange(min:Number, max:Number) : Number
		{
			var randomNum:Number = (Math.random() * (max - min )) + min;
			return randomNum;
		}
	}
}